package com.gt.web.srv.web.api;

import com.gt.web.api.common.R;
import com.gt.web.srv.annotation.login.NoAuthorization;
import com.gt.web.srv.domain.SsUser;
import com.gt.web.srv.interceptors.AuthUserContext;
import com.gt.web.srv.service.SsUserService;
import com.gt.web.srv.utils.BizUtils;
import com.gt.web.srv.utils.FileUtils;
import com.gt.web.srv.web.api.command.WxLoginReqCommand;
import com.gt.web.srv.web.api.request.UserRequest;
import com.gt.web.srv.web.api.resource.ResponseUserVo;
import com.gt.web.srv.web.api.resource.dto.TokenDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户接口
 */
@Slf4j
@RestController
@RequestMapping("/api/user/")
@Api(tags = "用户接口")
public class UserController {

    @Value("${web.upload-path}")
    private String uploadPath;
    @Value("${web.host-path}")
    private String showPath;


    @Autowired
    private SsUserService ssUserService;


    @NoAuthorization
    @PostMapping("/login")
    @ApiOperation(value = "小程序code登录接口", notes = "小程序code登录接口", position = 1)
    public R<ResponseUserVo> login(@Validated @RequestBody WxLoginReqCommand reqDto) {
        System.out.println(showPath);
        return ssUserService.login(reqDto);
    }

    @PostMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息接口", notes = "获取用户信息接口", position = 2)
    public R<SsUser> getUserInfo() {
        TokenDto tokenDto = AuthUserContext.get();
        SsUser userInfo = ssUserService.getByUserId(tokenDto.getUserId());
        return R.ok(userInfo);
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息", position = 3)
    public R<Boolean> updateUserInfo( @Validated @RequestBody UserRequest reqDto) {
        TokenDto tokenDto = AuthUserContext.get();
        reqDto.setUserId(tokenDto.getUserId());
        Boolean result = ssUserService.saveOrUpdateByUserId(reqDto);
        return R.ok(result);
    }


    @PostMapping(value="/uploadImage",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "上传头像", notes = "上传头像",position = 4)
    public R<List<String>> uploadImage(@RequestParam("file") MultipartFile[] files) {
        TokenDto tokenDto = AuthUserContext.get();
        String dir = BizUtils.datePath();
        List<String> urls = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            String fileName = FileUtils.upload(files[i], uploadPath+dir, files[i].getOriginalFilename());
            urls.add(showPath+dir+fileName);
        }
        ssUserService.updateWechatInfo(tokenDto.getUserId(), null, urls.get(0));
        return R.ok(urls);
    }
}
